package com.sky.controller.admin;

import com.github.pagehelper.Page;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetMealService;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/admin/setmeal")
public class SetMealController {
    @Autowired
    private SetMealService setMealService;
    /**
     * 套餐分页查询
     */
    @GetMapping("/page")
    public Result<PageResult> pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        log.info("前端传递的套餐分页查询参数：{}", setmealPageQueryDTO);
        PageResult page = setMealService.pageQuery(setmealPageQueryDTO);
        return Result.success(page);
    }

    /**
     * 新增套餐
     */
    @PostMapping
    @CacheEvict(cacheNames = "setMealCache", key = "#setmealDTO.categoryId")  // 删除单个菜品缓存.  key: setMealCache::categoryId
    public Result addSetMeal(@RequestBody SetmealDTO setmealDTO) {
        log.info("前端要新增的套餐：{}", setmealDTO);
        setMealService.addSetMeal(setmealDTO);
        return Result.success();
    }

    /**
     * 根据 ID 查询套餐
     */
    @GetMapping("/{id}")
    public Result<SetmealVO> selectSetMealDataById(@PathVariable Long id) {
        log.info("前端要查询的套餐ID：{}", id);
        SetmealVO setmealVO = setMealService.selectSetMealDataById(id);
        return Result.success(setmealVO);
    }

    /**
     * 批量删除套餐
     */
    @DeleteMapping
    @CacheEvict(cacheNames = "setMealCache", allEntries = true)  // 删除所有缓存
    public Result deleteSetMealBatch(@RequestParam List<Integer> ids) {
        log.info("前端要批量删除的套餐ID：{}", ids);
        setMealService.deleteSetMealBatch(ids);
        return Result.success();
    }

    /**
     * 套餐起售、停售
     */
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "setMealCache", allEntries = true)  // 删除所有缓存
    public Result setSetMealStatus(@PathVariable Integer status, Integer id) {
        log.info("前端要修改的状态为：{}", status);
        setMealService.setSetMealStatus(id, status);
        return Result.success();
    }

    /**
     * 修改套餐
     */
    @PutMapping
    @CacheEvict(cacheNames = "setMealCache", allEntries = true)  // 删除所有缓存
    public Result updateSetMeal(@RequestBody SetmealDTO setmealDTO) {
        log.info("前端修改后的套餐属性：{}", setmealDTO);
        setMealService.updateSetMeal(setmealDTO);
        return Result.success();
    }
}
